import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 * Created by zhangyilin on 2018/11/18.
 *
 * 学生加入班级
 */
public class addGroup extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setContentType("text/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        response.setCharacterEncoding("UTF-8");//setContentType与setCharacterEncoding的顺序不能调换，否则还是无法解决中文乱码的问
        String str="ADD The Class By The InviteID ";
        String title = new String(str.getBytes("ISO8859-1"),"UTF-8");
        String docType = "<!DOCTYPE html>\n";
        out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n");

        String inviteid=request.getParameter("inviteID");
        String stuid = (String) request.getSession().getAttribute("ID");
        out.print("<h3 align=\"center\">"+stuid+",you are joining in the class that the InviteID is:"+inviteid+"</h3>");
        out.println(" <br/>");

//        String answer = "0";
        try {
            //当学生加入班级时，把对应的班级码邀请信息塞到SClass表中学生对应的邀请码字段
            ADDInviteIDToSClass(inviteid, stuid, out);
            //当学生加入班级时，把对应的学生学号信息塞到CStudent表中班级邀请码对应的学生学号字段
            ADDStuIDToCStudent(inviteid, stuid, out);

//            out.write(answer);
//            out.close();
        }catch (Exception e) {
            out.print("get data error!");
            e.printStackTrace();
        }
    }
    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            //.out.println(e);
        }
        return conn;
    }

    //将学生加入的班级的邀请码插入SClass表的InviteID字段
    public void ADDInviteIDToSClass(String inviteID,String stuID,PrintWriter out) throws SQLException {
        //插入前，认为表中已经存在该学生的学号的记录
        Connection conn=  GetConnection();
        String str=inviteID+",";
        String sql="update sclass set InviteID=CONCAT(IFNULL(InviteID,''),?) where StudentID=?";
        PreparedStatement pst = null;
        pst = conn.prepareStatement(sql);
        pst.setString(1, str);
        pst.setString(2, stuID);
        int i=pst.executeUpdate();
        //为测试是否更新成功，可删除

        if(i!=0) {
            out.println("<center><p>测试Insert into SClass Success</p></center>");
        }
        //测试结束
        pst.close();
        conn.close();
    }
    //将加入班级的学生学号插入CStudent表的StudnetID字段

    public void ADDStuIDToCStudent(String inviteID,String stuID,PrintWriter out) throws SQLException {
        //插入前，表中已经存在该班级邀请码的条目
        Connection conn=  GetConnection();
        String sql="update CStudent set StudentID=CONCAT(IFNULL(StudentID,''),?) where InviteID=?";
        String str=stuID+",";
        PreparedStatement pst = null;
        pst = conn.prepareStatement(sql);
        pst.setString(1, str);
        pst.setString(2, inviteID);
        int i=pst.executeUpdate();
        //测试是否更新成功，可删除
        if(i!=0) {
            out.println("<center><p>Insert into CStudent Success</p></center>");
        }
        //测试结束
        pst.close();
        conn.close();


    }
}
